package com.atguigu.tms.mock.task;

import com.atguigu.mock.util.MyDateUtil;
import com.atguigu.mock.util.RanOpt;
import com.atguigu.mock.util.RandomBox;
import com.atguigu.tms.mock.bean.*;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.constant.TmsConstant;
import com.atguigu.tms.mock.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.atguigu.tms.mock.constant.TmsConstant.*;

@Component
public class PaymentMockTask {


    @Autowired
    OrderInfoService orderInfoService ;

    @Autowired
    ExpressTaskCollectService expressTaskCollectService;

    @Autowired
    ExpressCourierService expressCourierService;

    @Autowired
    OrderTraceLogService orderTraceLogService;

    @Autowired
    EmployeeInfoService employeeInfoService;

    @Autowired
    BaseOrganService baseOrganService;

    public void doPayment(Date curDate){
        RandomBox ifPayBox = new RandomBox(90, 10);
        List<OrderInfo> orderInfoList = orderInfoService.list(new QueryWrapper<OrderInfo>().eq("status", TmsConstant.ORDER_STATUS_ORDERED));
        for (OrderInfo orderInfo : orderInfoList) {
            if(ifPayBox.getRandBoolValue()){
                userPay(  curDate,  orderInfo);
            }else{
               if(  curDate.after(DateUtils.addMinutes(curDate,20))){  //当前时间超过订单之后20分钟
                   cancelOrder(  orderInfo,  curDate);
               }
            }
        }

    }

    private void cancelOrder(OrderInfo orderInfo,Date curDate){
        orderInfo.setUpdateTime(curDate);
        orderInfo.setStatus(ORDER_STATUS_CANCEL);
        orderInfoService.saveOrUpdate(orderInfo,true);
    }


    private void  userPay(Date curDate,OrderInfo orderInfo){
        orderInfo.setStatus(TmsConstant.ORDER_STATUS_PAID);


        RandomBox<String> paymentTypeBox=new RandomBox(new RanOpt(PAYMENT_TYPE_ALIPAY, AppConfig.payment_type_weight[0])
                ,new RanOpt(PAYMENT_TYPE_WECHAT, AppConfig.payment_type_weight[1]),
                new RanOpt(PAYMENT_TYPE_UNION,AppConfig.payment_type_weight[2]));
        String paymentType = paymentTypeBox.getRandStringValue();

        orderInfo.setPaymentType(paymentType);
        orderInfo.setUpdateTime(curDate);
        orderInfoService.saveOrUpdate(orderInfo);

        ExpressTaskCollect expressTaskCollect = genExpressTaskCollect(orderInfo);

        expressTaskCollectService.saveOrUpdate(expressTaskCollect);

        genOrderTraceLog(  orderInfo,  expressTaskCollect);

    }

    public ExpressTaskCollect  genExpressTaskCollect(OrderInfo orderInfo){

        ExpressTaskCollect expressTaskCollect = new ExpressTaskCollect();

        expressTaskCollect.setOrderId(orderInfo.getId());

        Long senderComplexId = orderInfo.getSenderComplexId();

        ExpressCourier expressCourier = expressCourierService.getCourierByComplex(EXPRESS_TYPE_COLLECT, senderComplexId);

        expressTaskCollect.setCourierEmpId(expressCourier.getEmpId());

        expressTaskCollect.setOrgId(expressCourier.getOrgId());

        BaseOrgan baseOrgan = baseOrganService.getById(expressCourier.getOrgId(), true);
        expressTaskCollect.setOrgName(baseOrgan.getOrgName());

        ExpressCourier courier = expressCourierService.getById(expressCourier.getEmpId(),true);

        expressTaskCollect.setCourierName(courier.getEmployeeInfo().getRealName());

        expressTaskCollect.setStatus(COLLECT_STATUS_ASSIGNED);

        //估计揽收时间
        Date estimateCollectTime = MyDateUtil.addHours(orderInfo.getCreateTime(), AppConfig.collect_estimate_handle_hour, true);

        expressTaskCollect.setEstimatedCollectedTime(estimateCollectTime);
        //估计提交时间
        Date estimateCommitTime = MyDateUtil.addHours(estimateCollectTime, AppConfig.commit_estimate_handle_hour, true);
        expressTaskCollect.setEstimatedCommitTime(estimateCommitTime );

        expressTaskCollect.setIsDeleted("0");
        expressTaskCollect.setCreateTime(orderInfo.getCreateTime());

        return  expressTaskCollect;

    }


    public void genOrderTraceLog(OrderInfo orderInfo,ExpressTaskCollect expressTaskCollect){
        OrderTraceLog orderTraceLog = new OrderTraceLog();
        orderTraceLog.setOrderId(orderInfo.getId());
        orderTraceLog.setTraceDesc("已支付，等待揽收");
        orderTraceLog.setCreateTime(orderInfo.getUpdateTime());
        orderTraceLog.setCurTaskId(expressTaskCollect.getId());
        orderTraceLog.setChargeEmpId(expressTaskCollect.getCourierEmpId());
        orderTraceLog.setTaskType(TASK_TYPE_COLLECT);
        orderTraceLog.setIsDeleted("0");
        orderTraceLogService.saveOrUpdate(orderTraceLog );
    }


}
